<?php
class ss_dao_product extends ss_dao_object{
	public function api_detail(){
		return $this->getData();
	}
	public function active($val=null){
		if(!is_null($val)) $this->active=($val?1:0);
		return ($this->active?true:false);
	}
	public function api_master(){
		return $this->detail();
		return array('name'=>$this->name(),'slug'=>$this->slug(),'$ref'=>$this->primary(),'idoffice'=>$this->primary());
		//return array('name'=>$this->name(),'slug'=>$this->slug(),'idoffice'=>$this->primary());
	}
	
	public function date_pub($date=null){
		if($date) $this->date_pub=$this->dateToMysql($date);
		return $this->date_pub;
	}
	
	public function date_rej($date=null){
		if($date) $this->date_rej=$this->dateToMysql($date);
		return $this->date_rej;
	}
}

class ss_dao_product_mapper extends ss_dao_mapper{
	protected $table='prod_detail';
	protected $primary='idprod_detail';
	
	public function api_master($query=null){
		$q=$this->injector->create('ss_dao_query');
		$sql=$q
		->from(array(
			$this->table))
		->where(array($query))
		->select();
		$stmt=$this->da->runStmt($sql,$q->values());
		return $this->getArray($stmt);
	}
	
	public function api_detail($id){
		$q=$this->injector->create('ss_dao_query');
		$sql=$q
		->from(array(
			$this->table))
		->where(array(
			array($this->primary=>$id))
			)
		->select();
		$stmt=$this->da->runStmt($sql,$q->values());
		return $stmt->fetchObject('ss_dao_product',array($this->primary));
	}
	
	private function getArray($stmt){
		$result=array();
		while($obj=$stmt->fetchObject('ss_dao_product',array($this->primary))){
			$result[$obj->{$this->primary}()]=$obj->clean();
		}
		$this->da->closeCursor($stmt);
		return $result;
	}
	
	public function query($query){
		return $this->master();
	}
	
	public function put($query,$body,$data){
		foreach($body as $key=>$val){
			$data->{$key}($val);
			//if($val!=$data->{$key}()) $result[$key]=$val;
		}
		$this->update($data);
		//ss_error::log($data);
		//ss_error::log($data->changed());
	}
	
	public function post($query,$body){
		//ss_error::log($body);
		$obj=new ss_dao_product($this->primary);
		$obj->setAttributes($body);
		//ss_error::log($obj);
		return $this->insert($obj);
	}
}
?>